package script


import cn.cms.util.ResponseUtil
import cn.cms.webcommon.ResBo
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.multipart.MultipartHttpServletRequest
import org.springframework.web.multipart.commons.CommonsMultipartResolver

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 仅做测试使用
 */
@Deprecated
class Test {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass())

    /**
     * 测试使用
     *
     * @param request
     * @param response
     */
    void getFile(HttpServletRequest request, HttpServletResponse response) {

        println(request.getParameter("name"));
        println(request.getParameter("age"));
        println(request.getParameter("json"));

        File file = new File("/Users/izhou/Documents/work/UC用户中心架构图.eddx");
        FileInputStream inputStream = new FileInputStream(file);

        String fileName = file.getName();
        response.reset();
        response.setCharacterEncoding("UTF-8");
        OutputStream outputStream = response.getOutputStream();
//        String userAgent = request.getHeader("User-Agent");
//        byte[] nameBytes = userAgent.contains("MSIE") ? fileName.getBytes()
//                : fileName.getBytes("UTF-8"); // fileName.getBytes("UTF-8")处理safari的乱码问题
//        fileName = new String(nameBytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
        response.setHeader("Content-disposition",
                String.format("attachment; filename=\"%s\"", new String(fileName.getBytes(), "ISO-8859-1")));
        response.setHeader("Content_Length", String.valueOf(file.length()));
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(bytes)) > -1) {
            outputStream.write(bytes, 0, len);
        }

        inputStream.close();
        outputStream.close();

    }

    /**
     * 测试使用
     *
     * @param request
     * @param response
     */
    void p(HttpServletRequest request, HttpServletResponse response) {

        println(request.getParameter("name"));
        println(request.getParameter("age"));
        println(request.getParameter("json"));

        response.reset();
        OutputStream outputStream = response.getOutputStream();
        String contextPath = request.getContextPath();

        String filePath = "/Users/izhou/projects/new_cms/src/main/webapp/WEB-INF/views/test.html";
        File file = new File(filePath);
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        StringBuilder sb = new StringBuilder();
        String tmp;
        while ((tmp = br.readLine()) != null) {
            sb.append(tmp);
        }
        outputStream.write(sb.toString().getBytes());

        outputStream.close();
    }

    /**
     * 测试使用
     *
     * @param request
     * @param response
     */
    void l(HttpServletRequest request, HttpServletResponse response) {

        println(request.getParameter("name"));
        println(request.getParameter("age"));
        println(request.getParameter("sex"));

        logger.info(request.getParameter("name"));
        logger.info(request.getParameter("age"));
        logger.info(request.getParameter("sex"));

        logger.info("request.getContextPath->" + request.getContextPath())
        logger.info("request.getServletPath->" + request.getServletPath())
        logger.info("request.getRequestURL->" + request.getRequestURL())
        logger.info("request.getRequestURI->" + request.getRequestURI())
        logger.info("request.getRealPath->" + request.getRealPath("application.properties"))
        logger.info("request.getRemoteAddr->" + request.getRemoteAddr())
        logger.info("request.getLocalAddr->" + request.getLocalAddr())
        logger.info("request.getRemoteUser->" + request.getRemoteUser())
        logger.info("request.getRemote->" + request.getRemoteHost() + ":" + request.getRemotePort())
        logger.info("request.getLocal->" + request.getLocalName() + ":" + request.getLocalPort())

        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if (commonsMultipartResolver.isMultipart(request)) {
            logger.info("yesyesyesy.......");
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;

            Iterator iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                MultipartFile multipartFile = multiRequest.getFile(iter.next());
                String fileName = multipartFile.getOriginalFilename();
                logger.info("upload filename: " + fileName);
                if (fileName == null || fileName.trim().equals("")) {
                    continue;
                }
                //20170207 针对IE环境下filename是整个文件路径的情况而做以下处理
                Integer index = fileName.lastIndexOf("\\");
                String newStr = "";
                if (index > -1) {
                    newStr = fileName.substring(index + 1);
                } else {
                    newStr = fileName;
                }
                if (!newStr.equals("")) {
                    fileName = newStr;
                }
                logger.info("new filename: " + fileName);

                if (multipartFile != null) {

                }
            }
        } else {
            logger.info("nonononono.......");
        }
        ResBo resBo = new ResBo();
        ResponseUtil.printAjax(response, resBo);

//        InputStream inputStream = request.getInputStream();
//        byte[] bytes = new byte[1024];
//        int len = 0;
//        len = inputStream.read(bytes);
//        byte[] finalbytes;
//        finalbytes = Arrays.copyOfRange(bytes,0,len);
//        System.out.println("body体:::"+new String(finalbytes,"UTF-8"));

//        request.getRequestDispatcher("/cms/Test-p.gv").forward(request, response);
//        response.reset();
//        OutputStream outputStream = response.getOutputStream();
//        String content = "<html>you are a good boy</html>";
//        outputStream.write(content.getBytes());
//
//        outputStream.close();
    }

    /**
     * 测试使用
     *
     * @param request
     * @param response
     */
    String h(HttpServletRequest request, HttpServletResponse response) {

        println(request.getParameter("name"));
        println(request.getParameter("age"));
        println(request.getParameter("sex"));

        logger.info(request.getParameter("name"));
        logger.info(request.getParameter("age"));
        logger.info(request.getParameter("sex"));

    }

}
